package com.bazhua.bazo.controller;

import com.bazhua.bazo.common.utils.JwtUtil;
import com.bazhua.bazo.common.wxapplet.model.WxLoginRequest;
import com.bazhua.bazo.config.JwtConfig;
import com.bazhua.bazo.repository.model.*;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2021-01-25 23:25
 **/
@Api(tags = "优惠券接口")
@RestController
@RequestMapping("/api/coupon")
public class CouponController {

    @Autowired
    private ICouponService couponService;

    @ApiOperation(value = "", notes = "设置优惠券")
    @RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult setCoupon(@RequestBody SetCoupon setCoupon, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.setCoupon(setCoupon, userId);
    }

    @ApiOperation(value = "", notes = "获取优惠券")
    @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getCoupon(Integer couponId, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.getCoupon(couponId, userId);
    }

    @ApiOperation(value = "draw", notes = "用户领取优惠券")
    @RequestMapping(value = "/draw", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult drawCoupon(@RequestBody DramCoupon dramCoupon, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.drawCoupon(dramCoupon, userId);
    }

    @ApiOperation(value = "check", notes = "核销用户优惠券")
    @RequestMapping(value = "/check", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult checkCoupon(@RequestBody CheckCoupon checkCoupon, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.checkCoupon(checkCoupon, userId);
    }

    @ApiOperation(value = "has", notes = "用户是否领取过优惠券")
    @RequestMapping(value = "/has", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult hasCoupon(@RequestParam Long couponId,  @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.hasCoupon(couponId, userId);
    }

    @ApiOperation(value = "audit", notes = "审核优惠券")
    @RequestMapping(value = "/audit", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult auditCoupon(@RequestParam Long couponId) {
        return couponService.auditCoupon(couponId);
    }

    @ApiOperation(value = "forbidden", notes = "禁用优惠券")
    @RequestMapping(value = "/forbidden", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult forbiddenCoupon(@RequestParam Long couponId) {
        return couponService.forbiddenCoupon(couponId);
    }

    @ApiOperation(value = "page", notes = "分页优惠券")
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult page(@RequestBody CouponSearch search) {
        return couponService.page(search, null);
    }

    @ApiOperation(value = "myCouponManage", notes = "发券管理")
    @RequestMapping(value = "/myCouponManage", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult myCouponManage(CouponSearch search, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.page(search, userId);
    }

    @ApiOperation(value = "myCouponPage", notes = "券包")
    @RequestMapping(value = "/myCouponPage", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getMyCouponPage(Integer pageNum, Integer pageSize, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.getMyCouponPage(pageNum, pageSize, userId);
    }

    @ApiOperation(value = "couponRecordDetail", notes = "券包明细")
    @RequestMapping(value = "/couponRecordDetail", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getCouponRecordDetail(Long couponRecordId, @RequestHeader String token) {
        Long userId = JwtUtil.getUserIdByToken(token);
        return couponService.getCouponRecordDetail(couponRecordId);
    }

    @ApiOperation(value = "couponRecordPage", notes = "券记录")
    @RequestMapping(value = "/couponRecordPage", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getCouponRecordPage(CouponRecordSearch couponRecordSearch) {
        return couponService.getCouponRecordPage(couponRecordSearch);
    }
}
